package cn.fanyamin;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sun.misc.Signal;
import sun.misc.SignalHandler;
import cn.fanyamin.server.AbstractServer;
import cn.fanyamin.server.AbstractService;
import cn.fanyamin.server.IServer;
import cn.fanyamin.server.IService;

/**
 * @author walter fan ya min
 * 
 *         TODO: JMS service create, send, receive, transaction testing
 * 
 */
public class Starter extends AbstractService implements IService {
    private static Log logger = LogFactory.getLog(Starter.class);
    private List<IService> serverList = new ArrayList<IService>(10);
    private volatile boolean stopRequested = false;

    public Starter() {
        Signal.handle(new Signal("INT"), new SignalHandler() {
            public void handle(Signal sig) {
                System.out.println("Got a signal--" + sig);
                try {
                    stop();
                    clean();
                } catch (Exception e) {
                    logger.error(e);
                }

            }
        });
    }
    
    @Override
    public String getName() {
        return "starter";
    }

    @Override
    public void onInit() throws Exception {
        ListIterator<IService> it = serverList.listIterator();
        while (it.hasNext()) {
            it.next().init();
        }

    }
    
    @Override
    public void onClean() throws Exception {
        ListIterator<IService> it = serverList.listIterator();
        while (it.hasNext()) {
            it.next().clean();
        }

    }
    
    @Override
    public void onStart() throws Exception {
        ListIterator<IService> it = serverList.listIterator();
        while (it.hasNext()) {
            it.next().start();
        }

    }

    @Override
    public void onStop() throws Exception {
        ListIterator<IService> it = serverList.listIterator(serverList.size());

        while (it.hasPrevious()) {
            it.previous().stop();
        }

    }

    public void addServer(IService svr) {
        this.serverList.add(svr);
    }

    public void removeServer(IServer svr) {
        ListIterator<IService> it = serverList.listIterator();
        while (it.hasNext()) {
            if (it.next().getName().equals(svr.getName())) {
                it.remove();
            }
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("--- start ---");
        int argc = args.length;
        final Starter mgr = new Starter();
        
        switch(argc) {
        case 1:
            if ("start".equals(args[0])) {
                mgr.startup();
            } else if("stop".equals(args[0])) {
                mgr.shutdown();
            }
            break;
        default:
                break;
        }
        
        
    }
    
    private void shutdown() {
        this.stopRequested = true;
        try {
            this.stop();
            this.clean();
        } catch (Exception e1) {
            logger.error(e1);
        }
        
    }

    private void startup() {
        try {
            init();
            start();
        } catch (Exception e1) {
            logger.error(e1);
        }

       
    }

}
